#import "BYButton.h"
@interface BYButton ()
@property (nonatomic, strong) UIView *seprateLine;
@end
@implementation BYButton
- (instancetype)initWithFrame:(CGRect)frame
                  iconNameNor:(NSString *)iconNameNor
                  iconNameSel:(NSString *)iconNameSel
                         text:(NSString *)text
                         font:(UIFont *)font
                 textColorNor:(UIColor *)textColorNor
                 textColorSel:(UIColor *)textColorSel
                        style:(ButtonLayoutStyle)style
                        inset:(CGFloat)inset
                        space:(CGFloat)space
                   isAnimated:(BOOL)isAnimated
{
    if (self = [super initWithFrame:frame]) {
        _iconNameNor = iconNameNor;
        _iconNameSel = iconNameSel;
        _text = text;
        _textColorNor = textColorNor;
        _textColorSel = textColorSel;
        _myfont = font;
        _style = style;
        _inset = inset;
        _space = space;
        _isAnimated = isAnimated;
        [self setSubViews];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
                  iconName:(NSString *)iconName
                         text:(NSString *)text
                         font:(UIFont *)font
                 textColor:(UIColor *)textColor
                        style:(ButtonLayoutStyle)style
                        inset:(CGFloat)inset
                        space:(CGFloat)space
{
    if (self = [super initWithFrame:frame]) {
        _iconNameNor = iconName;
        _iconNameSel = iconName;
        _text = text;
        _textColorNor = textColor;
        _textColorSel = textColor;
        _myfont = font;
        _style = style;
        _inset = inset;
        _space = space;
        _isAnimated = NO;
        [self setSubViews];
    }
    return self;
}
- (instancetype)init {
    if (self = [super init]) {
        [self setSubViews];
    }
    return self;
}
- (void)setSubViews
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickSelf)];
    [self addGestureRecognizer:tap];
    [self addSubview:self.iconView];
    [self addSubview:self.textLabel];
}
- (void)clickSelf {
    if (self.clickBlock) {
        self.clickBlock();
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(byButtonClick)]) {
        [self.delegate byButtonClick];
    }
}
- (void)layoutSubviews {
    [super layoutSubviews];
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.top.mas_equalTo(_inset);
        make.bottom.mas_equalTo(-_inset);
        make.width.mas_equalTo(self.iconView.mas_height);
        if (self.style == ButtonLayoutStyleNormal) {
            make.left.mas_equalTo(_inset);
        } else {
            make.right.mas_equalTo(-_inset);
        }
    }];
    [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        if (self.style == ButtonLayoutStyleNormal) {
            make.right.mas_equalTo(-_inset);
            make.left.mas_equalTo(self.iconView.mas_right).offset(_space);
        } else {
            make.left.mas_equalTo(_inset);
            make.right.mas_equalTo(self.iconView.mas_left).offset(-_space);
        }
    }];
}
- (void)setText:(NSString *)string {
    self.textLabel.text = string;
    [self layoutIfNeeded];
}
- (void)setIsSelected:(BOOL)isSelected
{
    _isSelected = isSelected;
    if (isSelected) {
        [self setIocn:self.iconNameSel text:self.textLabel.text textColor:self.textColorSel];
    }else{
        [self setIocn:self.iconNameNor text:self.textLabel.text textColor:self.textColorNor];
    }
}
- (void)setIocn:(NSString *)icon text:(NSString *)text textColor:(UIColor *)color
{
    if (_isAnimated) {
        [UIView animateKeyframesWithDuration:0.5 delay:0 options:UIViewKeyframeAnimationOptionLayoutSubviews animations:^{
            [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:1/2.0 animations:^{
                self.iconView.transform = CGAffineTransformMakeScale(1.6, 1.6);
            }];
            [UIView addKeyframeWithRelativeStartTime:1/2.0 relativeDuration:1/2.0 animations:^{
                self.iconView.transform = CGAffineTransformIdentity;
            }];
        } completion:^(BOOL finished) {
        }];
    }
    self.iconView.image = [UIImage imageNamed:icon];
    self.textLabel.text = text;
    self.textLabel.textColor = color;
}
#pragma mark - get\set
- (UIImageView *)iconView
{
    if (_iconView == nil) {
        _iconView = [[UIImageView alloc] init];
        _iconView.image = [UIImage imageNamed:self.iconNameNor];
        _iconView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _iconView;
}
- (UILabel *)textLabel
{
    if (_textLabel == nil) {
        _textLabel = [[UILabel alloc] init];
        _textLabel.font = self.myfont;
        _textLabel.textAlignment = NSTextAlignmentLeft;
        _textLabel.textColor = self.textColorNor;
        _textLabel.text = self.text;
    }
    return _textLabel;
}
- (UIView *)seprateLine {
    if (_seprateLine == nil) {
        _seprateLine = [[UIView alloc] init];
        _seprateLine.backgroundColor = [UIColor clearColor];
    }
    return _seprateLine;
}
@end
